/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sembr.vtk.visualizing;

import vtk.vtkPanel;
import vtk.vtkRenderer;
import vtk.vtkRendererCollection;

/**
 *
 * @author Adriano
 */
public class VtkMultRenderPanel extends vtkPanel
{
    private vtkRenderer renderer2;
    private vtkRenderer renderer3;
    private vtkRenderer renderer4;
    private vtkRendererCollection rendererCollection;
            
    public VtkMultRenderPanel()
    {
        // top-right renderer
        renderer2 = new vtkRenderer();
        // bottom-left renderer
        renderer3 = new vtkRenderer();
        // bottom-right renderer
        renderer4 = new vtkRenderer();           
        // collection of renderers
        rendererCollection = new vtkRendererCollection();
        // set renderers´ background
        setRenderersBackground(255.00, 255.00, 255.00);
        // reset cameras for correct display
        //resetCameras();
    }  

    /**
     * @return the render2
     */
    public vtkRenderer getRenderer2() 
    {
        return renderer2;
    }

    /**
     * @return the render3
     */
    public vtkRenderer getRenderer3() 
    {
        return renderer3;
    }

    /**
     * @return the render4
     */
    public vtkRenderer getRenderer4() 
    {
        return renderer4;
    }
      
    public void setRenderer1Background(double red, double green, double blue)
    {
        super.ren.SetBackground(red, green, blue);
    }
    
    public void setRenderer2Background(double red, double green, double blue)
    {
        renderer2.SetBackground(red, green, blue);
    }
    
    public void setRenderer3Background(double red, double green, double blue)
    {
        renderer3.SetBackground(red, green, blue);
    }
    
    public void setRenderer4Background(double red, double green, double blue)
    {
        renderer4.SetBackground(red, green, blue);
    }
        
    public void setRenderWindow(boolean tl, boolean tr, boolean bl, boolean br)
    {
        updateRenderers(tl, tr, bl, br);
             
        for (int i = 0; i < rendererCollection.GetNumberOfItems(); ++i)
        {
            vtkRenderer auxRen = (vtkRenderer)rendererCollection.GetItemAsObject(i);            
            super.rw.AddRenderer(auxRen);
        }
    }
    
    private void updateRenderers(boolean tl, boolean tr, boolean bl, boolean br)
    {
        if (rendererCollection.GetNumberOfItems() != 0)
        {
            for (int i = 0; i < rendererCollection.GetNumberOfItems(); ++i)
            {
                vtkRenderer auxRen = (vtkRenderer)rendererCollection.GetItemAsObject(i);  
                super.rw.RemoveRenderer(auxRen);
            }
            
            rendererCollection.RemoveAllItems();
        }
        // renderer1 and renderer3
        // renderers´ vertical configuration
        if ((tl && tr) && !(bl && br))
        {
            rendererCollection.AddItem(super.ren);
            rendererCollection.AddItem(renderer2);
            // configure renderers´s display
            super.ren.SetViewport(0.00, 0.00, 0.499, 1.00);
            renderer2.SetViewport(0.501, 0.00, 1.00, 1.00);  
        }
        // renderers´ horizontal configuration
        else if ((tl && bl) && !(tr && br))
        {
            rendererCollection.AddItem(super.ren);
            rendererCollection.AddItem(renderer3);
            // configure renderers´s display
            super.ren.SetViewport(0.00, 0.00, 1.00, 0.499);
            renderer3.SetViewport(0.00, 0.501, 1.00, 1.00);            
        }
        // all four renderers will be displayed
        else if (tl && tr && bl && br)
        {
            rendererCollection.AddItem(super.ren);
            rendererCollection.AddItem(renderer2);
            rendererCollection.AddItem(renderer3);
            rendererCollection.AddItem(renderer4);
            // configure renderers´s display
            super.ren.SetViewport(0.00, 0.00, 0.499, 0.499);
            renderer2.SetViewport(0.501, 0.00, 1.00, 0.499);
            renderer3.SetViewport(0.00, 0.501, 0.499, 1.00);
            renderer4.SetViewport(0.501, 0.501, 1.00, 1.00);
        }
        else
        {
            rendererCollection.AddItem(super.ren);
            super.ren.SetViewport(0.00, 0.00, 1.00, 1.00);
        }
        
        rendererCollection.SetGlobalWarningDisplay(0);
        resetCameras();
    } 
    
    /**
     * set renderers´ background
     * TODO: this method may be changed, color is set by hard code
     * @param red
     * @param green
     * @param blue 
     */
    private void setRenderersBackground(double red, double green, double blue)
    {
        setRenderer1Background(red, green, blue);
        setRenderer2Background(red, green, blue);
        setRenderer3Background(red, green, blue);
        setRenderer4Background(red, green, blue);
    }
    
    private void resetCameras()
    {
        super.ren.ResetCamera();
        renderer2.ResetCamera();
        renderer3.ResetCamera();
        renderer4.ResetCamera();
    }
}
